home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / ascii2fax / X2PPbitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.1 KB  |  147 lines

  1. /* Ximage2PPbitmap.c: converts as X image to a PP bitmap */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/X2PPbitmap.c,v 6.0 1991/12/18 20:15:19 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/X2PPbitmap.c,v 6.0 1991/12/18 20:15:19 jpo Rel $
  9.  *
  10.  * $Log: X2PPbitmap.c,v $
  11.  * Revision 6.0  1991/12/18  20:15:19  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include    "util.h"
  17. #include    <stdio.h>
  18. #include     <X11/Xlib.h>
  19. #include     <X11/Xutil.h>
  20. #include        <X11/Intrinsic.h>
  21. #include        <X11/StringDefs.h>
  22. #include        <X11/Shell.h>
  23. #include    "fonts.h"
  24. #include    <isode/cmd_srch.h>
  25.  
  26. #define     OPT_IN    1
  27. #define        OPT_OUT 2
  28.  
  29. CMD_TABLE    tbl_options [] = {
  30.     "-in",    OPT_IN,
  31.     "-out",    OPT_OUT,
  32.     0,    -1,
  33. };
  34.  
  35. typedef struct _cmdLineResources {
  36.         char    *in;
  37.     char    *out;
  38. } CmdLineResources;
  39.  
  40. static CmdLineResources cmdLine_resources;
  41. extern BitMap    new_bitmap();
  42.  
  43. main(argc, argv)
  44. int    argc;
  45. char    **argv;
  46. {      
  47.     Arg     arg[1];
  48.     Display    *display;
  49.     XtAppContext    appContext;
  50.     int    wid, ht, hot_x, hot_y;
  51.     Pixmap    pixmap;
  52.     XImage    *image;
  53.     unsigned long    black, white;
  54.     int    row, col, i;
  55.     FILE    *fp;
  56.     BitMap    bitmap;
  57.     char    *ix;
  58.  
  59.     for (i = 1; i < argc; i++) {
  60.         switch (cmd_srch(argv[i], tbl_options)) {
  61.             case OPT_IN:
  62.             if (++i >= argc) {
  63.                 printf("No input file given with flag '%s'\n",
  64.                        argv[i-1]);
  65.                 exit (1);
  66.             }
  67.             cmdLine_resources.in = strdup(argv[i]);
  68.             break;
  69.             case OPT_OUT:
  70.             if (++i >= argc) {
  71.                 printf("No output file given with flag '%s'\n",
  72.                        argv[i-1]);
  73.                 exit (1);
  74.             }
  75.             cmdLine_resources.out = strdup(argv[i]);
  76.             break;
  77.             default:
  78.             printf("Unknown flag '%s'\n",
  79.                    argv[i]);
  80.             exit(1);
  81.         }
  82.     }
  83.  
  84.     if (cmdLine_resources.in == NULLCP) {
  85.         printf("No input file given\n");
  86.         exit(1);
  87.     }
  88.  
  89.     XtToolkitInitialize();
  90.  
  91.         appContext = XtCreateApplicationContext();
  92.  
  93.         display = XtOpenDisplay(appContext,
  94.                  (String) NULL,
  95.                  (String) argv[0],
  96.                  (String) "PPCONVERTERS",
  97.                              NULL, 0,
  98.                  &argc,
  99.                  argv);
  100.  
  101.     if (display == NULL) {
  102.                 printf("%s\n", "unable to open display");
  103.                 exit(1);
  104.         }
  105.     
  106.     black = XBlackPixel(display, XDefaultScreen(display));
  107.     white = XWhitePixel(display, XDefaultScreen(display));
  108.  
  109.     if (XReadBitmapFile(display, 
  110.                 XRootWindow(display, XDefaultScreen(display)),
  111.                 cmdLine_resources.in, &wid, &ht,
  112.                 &pixmap, &hot_x, &hot_y) != BitmapSuccess) {
  113.         printf("Unable to create pixmap from bitmap file '%s'\n",
  114.                cmdLine_resources.in);
  115.         exit(1);
  116.     }
  117.  
  118.     image = XGetImage (display,
  119.                pixmap,
  120.                0,
  121.                0,
  122.                wid, ht,
  123.                XAllPlanes(),
  124.                XYPixmap);
  125.     
  126.     bitmap = (BitMap) new_bitmap(wid, ht);
  127.     
  128.     for (row = 0; row < ht; row++) {
  129.         for (col = 0; col < wid; col++) {
  130.             if (XGetPixel(image, col, row) == white) 
  131.                 BL_CLR(col, bitmap[row]);
  132.             else
  133.                 BL_SET(col, bitmap[row]);
  134.         }
  135.     }
  136.  
  137.     if (cmdLine_resources.out == NULL)
  138.         fp = stdout;
  139.     else if ((fp = fopen(cmdLine_resources.out, "w")) == NULL) {
  140.         printf("unable to open output file '%s'\n", 
  141.                cmdLine_resources.out);
  142.         exit(1);
  143.     }
  144.     bitmap2file (fp, bitmap, wid, ht);
  145.     fclose(fp);
  146. }
  147.